﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using FineUI;
using Newtonsoft.Json;
using ZHDJ.Core;
using ZHDJ.Core.Util;
using ZHDJ.Data.BLL;
using ZHDJ.Data.Models.Center;
using ZHDJ.Data.Models.Common;
using ZHDJ.WebPage;
using ZHDJ.WebPage.Master;

namespace ZHDJ.Web.Portal.PartyRepresentative
{
    public partial class MeetingWith : UseMorePageMasterPage
    {
        /// <summary>
        /// 是否启用picker
        /// </summary>
        public override bool IsEnabledPicker
        {
            get { return true; }

        }
        /// <summary>
        /// 工作室ID
        /// </summary>
        private string StudioID
        {
            get
            {
                return this.Request["StudioID"] ?? "null";
            }
        }
        /// <summary>
        /// 党代表ID
        /// </summary>
        private string RepresentativeID
        {
            get
            {
                return this.Request["RepresentativeID"] ?? "null";
            }
        }

        private string QueryText
        {
            get
            {
                return this.Request["QueryText"] ?? "";
            }
        }
        private string QueryStatus
        {
            get
            {
                return this.Request["QueryStatus"] ?? "";
            }
        }
        /// <summary>
        /// 页数
        /// </summary>
        private string PageIndex
        {
            get
            {
                return this.Request["PageIndex"] ?? "";
            }
        }

        /// <summary>
        /// 每页条数
        /// </summary>
        private string PageSize
        {
            get
            {
                return this.Request["PageSize"] ?? "";
            }
        }
        private string DataCount
        {
            get
            {
                return this.Request["DataCount"] ?? "";
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
           
            if (!string.IsNullOrEmpty(this.PageIndex))
            {
                this.WriteList();
            }
            else if (!string.IsNullOrEmpty(this.DataCount))
            {
                WriteDataCount();

            }
            if (!IsPostBack)
            {
                CheckLogin(true);
                LoadData();
            }

        }

        public void LoadData()
        {
            var studio = DB.PartyRepresentativeStudios.Find(StudioID);
            if (studio == null)
            {
                RedirectToDefault();
            }
            ControlBusiness.ControlBind(ddlRepresentativeID,
                DB.PartyRepresentativeInfos.Where(a => a.StudioID == StudioID), "UserName", "ID", true);
            this.hdStudioID.Value = StudioID;
            txtUserName.Text = CurrentUser.UserName;
            txtMobileNo.Text = CurrentUser.MobileNo;

            var representative = DB.PartyRepresentativeInfos.Find(RepresentativeID);
            if (representative != null)
            {
                ddlRepresentativeID.SelectedValue = RepresentativeID;
            }

        }

        #region 获取约见记录
        private void WriteList()
        {
            DataTable dt = PartyRepresentativeMeetingWith.GetGridData(Convert.ToInt32(this.PageIndex), Convert.ToInt32(PageSize), QueryStatus, QueryText,StudioID).Tables[0];

           
            dt.Columns.Add("CreateTime2", typeof(string));
            foreach (DataRow dr in dt.Rows)
            {
                dr["CreateTime2"] = Convert.ToDateTime(dr["CreateTime"]).ToString("yyyy-MM-dd HH:mm");
            }
            var json = JsonConvert.SerializeObject(dt);
            Response.Clear();
            Response.Write(json);
            Response.End();
        }
        private void WriteDataCount()
        {
            Response.Clear();
            Response.Write(PartyRepresentativeMeetingWith.GetGridData(0, 1, QueryStatus, QueryText, StudioID).Tables[1].Rows[0][0].ToString());
            Response.End();
        }
        #endregion

        #region 提交约见

        public bool CheckValue()
        {

            var rs = true;
           
            if (this.txtTitle.Text.Trim() == "")
            {
                this.txtTitle.MarkInvalid("必填");
                rs = false;
            }
            if (this.txtUserName.Text.Trim() == "")
            {
                this.txtUserName.MarkInvalid("必填");
                rs = false;
            }
            if (this.txtMobileNo.Text.Trim() == "")
            {
                this.txtMobileNo.MarkInvalid("必填");
                rs = false;
            }
            if (this.txtPost.Text.Trim() == "")
            {
                this.txtPost.MarkInvalid("必填");
                rs = false;
            }
            if (this.txtWorkUnit.Text.Trim() == "")
            {
                this.txtWorkUnit.MarkInvalid("必填");
                rs = false;
            }
            if (this.txtContent.Text.Trim() == "")
            {
                this.txtContent.MarkInvalid("必填");
                rs = false;
            }
            if (this.ddlRepresentativeID.SelectedValue == "")
            {
                this.ddlRepresentativeID.MarkInvalid("必填");
                rs = false;
            }
            if (!KeyWord.IsContainSensitiveWords(this.txtTitle.Text))
            {
                this.txtTitle.MarkInvalid("您输入的主题存在敏感词");
                rs = false;
            }

            if (!KeyWord.IsContainSensitiveWords(this.txtContent.Text))
            {
                this.txtContent.MarkInvalid("您输入的反映内容存在敏感词");
                rs = false;
            }
            return rs;

        }
       

        public void SaveData()
        {
            if (string.IsNullOrEmpty(StudioID))
            {
                PageContext.RegisterStartupScript(FineUI.Alert.GetShowReference("参数错误。", "提示", "su();"));
                return;
            }
            if (!CheckValue())
                return;
            var saveModel = new PartyRepresentativeMeetingWith();
            saveModel.ID = StringHelper.NewID;
            saveModel.StudioID = StudioID;
            saveModel.WithBy = CurrentUser.LoginID;

            saveModel.RepresentativeID = ddlRepresentativeID.SelectedValue;
            saveModel.SerialNumber = "";
            saveModel.Title = txtTitle.Text;
            saveModel.MobileNo = txtMobileNo.Text;
            saveModel.Post = txtPost.Text;
            saveModel.WorkUnit = txtWorkUnit.Text;
            saveModel.Content = txtContent.Text;
            saveModel.Status = 1;
            saveModel.MeetingID = "";
            saveModel.CreateTime = DateTime.Now;
            saveModel.CreateBy = CurrentUser.LoginID;
            saveModel.ModifyTime = DateTime.Now;
            saveModel.ModifyBy = CurrentUser.LoginID;
            DB.PartyRepresentativeMeetingWiths.Add(saveModel);
            if (DB.SaveChanges() > 0)
            {
                PageContext.RegisterStartupScript(FineUI.Alert.GetShowReference("预约信息已提交。", "提示", "su();"));
                var studio = DB.PartyRepresentativeStudios.Find(saveModel.StudioID);
                //添加待办
                if (studio.Category == "1")
                {
                    //组织
                    PartyTaskHelper.AddTaskToBranch(saveModel.ID, "预约党代表", SysEnum.TaskCategory.PartyRepresentative,
                        saveModel.CreateBy, new string[] {studio.ObjectID},
                        "../../../../App/BranchCenter/Module/PartyRepresentative/PartyRepresentativeMeetingWithEdit.aspx?id=" +
                        saveModel.ID+ "&StudioID=" + saveModel.StudioID);
                }
                else
                {
                    //个人
                    PartyTaskHelper.AddTaskToMember(saveModel.ID, "预约党代表", SysEnum.TaskCategory.PartyRepresentative,
                       saveModel.CreateBy, new string[] { studio.ObjectID },
                       "../../../../App/BranchCenter/Module/PartyRepresentative/PartyRepresentativeMeetingWithEdit.aspx?id=" +
                        saveModel.ID + "&StudioID=" + saveModel.StudioID);
                }
            }
            else
            {
                FineUI.Alert.Show("提交失败，请稍后再试。");
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            SaveData();
        }
        #endregion
        #region 模块菜单
        /// <summary>
        /// 初始化module  如果通过url传参可不设置
        /// </summary>
        protected override void InitModule()
        {
            Category = "PartyRepresentativeMeetingWith";
        }
        #endregion


    }
}